package test.crane;

import ptolemy.actor.*;
import ptolemy.kernel.*;
import ptolemy.kernel.util.*;
import ptolemy.data.*;
import ptolemy.data.type.*;

//////////////////////////////////////////////////////////////////////////
//// GenStimulus
/**

@author Ivan Jeukens
@version v 0.1 07/02/2002
*/
public class GenStimulus extends TypedAtomicActor {

    public GenStimulus(CompositeEntity container, String name) throws
            IllegalActionException, NameDuplicationException {
        super(container, name);

        shutdown = new TypedIOPort(this, "shutdown", false, true);
        shutdown.setTypeEquals(BaseType.GENERAL);

        powerOn = new TypedIOPort(this, "powerOn", false, true);
        powerOn.setTypeEquals(BaseType.GENERAL);
        
        fd = new TypedIOPort(this, "fd", false, true);
        fd.setTypeEquals(BaseType.DOUBLE);
        
        posdesired = new TypedIOPort(this, "posdesired", false, true);
        posdesired.setTypeEquals(BaseType.DOUBLE);
    }

    public TypedIOPort shutdown;
    public TypedIOPort powerOn;
    public TypedIOPort fd;
    public TypedIOPort posdesired;

    ///////////////////////////////////////////////////////////////////
    ////                         public methods                    ////

    public void initialize() throws IllegalActionException {
        super.initialize();
        ((Director)getDirector()).fireAt(this, 0);
        _state = BEGIN;
    }
    
    public void fire() throws IllegalActionException {

        Director d = getDirector();
        
        switch(_state) {
            case BEGIN: {
                powerOn.broadcast(new Token());
                fd.broadcast(new DoubleToken(0.0));
                _state = POS1;
                System.out.println("Liguei tudo!");
                d.fireAt(this, 0.1);
            } break;

            case POS1: {
                posdesired.broadcast(new DoubleToken(3.5));
                d.fireAt(this, d.getCurrentTime() + 20);
                 _state = POS2;
            } break;

            case POS2: {
                posdesired.broadcast(new DoubleToken(3.7));
                d.fireAt(this, d.getCurrentTime() + 5);
                _state = S3;
            } break;

            case S3: {
                posdesired.broadcast(new DoubleToken(-3.5));
                d.fireAt(this, d.getCurrentTime() + 4);
                _state = S4;
            } break;
            
            case S4: {
                fd.broadcast(new DoubleToken(400));
                d.fireAt(this, d.getCurrentTime() + 1);
                _state = S5;
            } break;
            
            case S5: {
                fd.broadcast(new DoubleToken(0));
                d.fireAt(this, d.getCurrentTime() + 20);
                _state = S6;
            } break;
            
            case S6: {
                posdesired.broadcast(new DoubleToken(3.5));
                d.fireAt(this, d.getCurrentTime() + 3);
                _state = S7;
            } break;
            
            case S7: {

                _state = S8;
            } break;
            
            case S8: {
                posdesired.broadcast(new DoubleToken(3.7));
                d.fireAt(this, d.getCurrentTime() + 20);
                _state = S9;
            } break;
            
            case S9: {
                posdesired.broadcast(new DoubleToken(-4.9));
                d.fireAt(this, d.getCurrentTime() + 20);
                _state = END;
            } break;

            case END: {
                shutdown.broadcast(new Token());
            } break;
        }
    }

    ///////////////////////////////////////////////////////////////////
    ////                        private methods                    ////

    ///////////////////////////////////////////////////////////////////
    ////                      private variables                    ////

    private int _state;
    
    
    private static final int BEGIN = 1;
    private static final int POS1 = 2;
    private static final int POS2 = 3;
    private static final int S3 = 4;
    private static final int S4 = 5;
    private static final int S5 = 6;
    private static final int S6 = 7;
    private static final int S7= 8;
    private static final int S8 = 9;
    private static final int S9 = 10;
    
    private static final int END = 99999;
    
}
